const BASEURL = "http://localhost:8080";

const fetch = (options) => {
	return new Promise((resolve, rejects) => {
		if (options.method === 'upload') {
			handleUpdateFile(options, resolve, rejects)
		} else {
			handleRequest(options, resolve, rejects)
		}
	})
};

// 发起请求
function handleRequest(options, resolve, reject) {
	const token = uni.getStorageSync('token')
	// console.warn("token", whiteList(options), token);
	uni.showLoading({
		title: "加载中"
	});
	if (!token && !whiteList(options)) {
		// 执行没有登录的逻辑
		uni.reLaunch({
			url: '/pages/login/index'
		})
		console.log('执行没有登录的逻辑')
		uni.hideLoading()
		return;
	}
	if (whiteList(options)) {
		return uni.request({
			url: BASEURL + options.url,
			method: options.method,
			data: options.data,
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				...options.headers,
			},
			success: (response) => {
				if (response.statusCode == 401) {
					return uni.showToast({
						icon: 'error',
						position: 'top',
						title: response.data && response.data.msg || '出错了，请稍后重试！'
					});
				}
				return resolve(response.data)
			},
			fail: (err) => {
				return reject(err);
			},
			complete() {
				uni.hideLoading();
			}
		})
	}
	uni.request({
		url: BASEURL + options.url,
		method: options.method,
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
			...options.headers,
			Authorization: 'Bearer ' + token
		},
		data: options.data,
		success: (response) => {
			if (response.statusCode == 401) {
				uni.showToast({
					icon: 'error',
					position: 'top',
					title: '出错了，请尝试重新登录！'
				});
				if (response.data.code == 'A0230') {
					uni.removeStorage({
						key: 'token'
					})
					uni.reLaunch({
						url: '/pages/login/index'
					})
				}
				return
			}
			return resolve(response.data)
		},
		fail: (err) => {
			console.log('error', err)
			return reject(err);
		},
		complete() {
			uni.hideLoading();
		}
	})
}
// 发起请求
function handleUpdateFile(options, resolve, reject) {
	const token = uni.getStorageSync('token')
	uni.showLoading({
		title: "上传中"
	})
	if (!token) {
		uni.reLaunch({
			url: '/pages/login/index'
		})
		console.log('未登录')
		uni.hideLoading()
		return;
	}
	if (options.type === 'array') {
		return uni.showToast({
			icon: 'error',
			position: 'top',
			title: '暂不支持多文件上传'
		});
	}
	uni.uploadFile({
		url: BASEURL + "/api/v1/files",
		name: 'file',
		// 单文件上传
		filePath: options.file,
		// 多文件上传
		// files: options.files, // Array
		header: {
			Authorization: 'Bearer ' + token,
			// 'Content-Type': 'multipart/form-data',
		},
		success: (response) => {
			if (response.statusCode == 401) {
				if (response.data.code == 'A0230') {
					uni.removeStorage({
						key: 'token'
					})
					uni.reLaunch({
						url: '/pages/login/index'
					})
				}
				return uni.showToast({
					icon: 'error',
					position: 'top',
					title: '出错了，请尝试重新登录！'
				});
			}
			return resolve(response.data)
		},
		fail: (err) => {
			console.log('error', err)
			return reject(err);
		},
		complete() {
			uni.hideLoading();
		}
	});
}

// 白名单
function whiteList(config) {
	let list = [
		(config.url.indexOf('/api/v1/auth') > -1),
		(config.url == '/api/v1/users/registry' && config.method.toUpperCase() == 'POST'),
	]
	let bol = false
	for (const item of list) {
		if (item) {
			bol = true
		}
	}
	return bol
}

export default fetch;